/*
* 通用JS表单验证类
* @Ethan - youed.me

* 可验证的数据类型
***** 空值/非法字符/邮箱地址/http网址/QQ号/座机号码/手机号码/身份证号/安全字符/重复输入/字符长度范围/数字类型/中文
* 对应的函数名
***** required/illegal/mail/url/qq/telePhone/mobilePhone/idCard/safeStr/repeat/range(min,max)/number/chinese
*/

vForm = {
	required : /.+/, //对需正则表达式验证数据类型进行定义
	illegal : /^[a-z]\w{3,}$/i,
	chinese : /^[\u0391-\uFFE5]+$/,
	mail : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
	number : /^\d+$/,
	qq : /^[1-9]\d{4,8}$/,
	telePhone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,
	mobilePhone : /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/,
	idCard : "this.IsIdCard(value)",
	safeStr : "this.IsSafe(value)",
	UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
	IsSafe : function(str){return !this.UnSafe.test(str);},
	repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",
	range : "getAttribute('min') < (value|0) && (value|0) < getAttribute('max')",
	valid : function(formID) {
		var obj = document.getElementById(formID); //获取表单内所有元素，这里obj是一个数组
		for (i=0;i<obj.length;i++) { //根据数组长度创建一个循环
			with(obj.elements[i]) { //使用with方法设置默认对象
				var _dataType = getAttribute("dataType"); //获取表单的验证数据类型，这里的getAttribute等同于obj.elements[i].getAttribute
				var _require = getAttribute("require");   //表单是否必须，值为0，1
				var _message =  getAttribute("msg");      //获取表单提示信息
				if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined")  continue;
				this.ClearState(obj.elements[i]);
				if(_require == "0" && value == "") continue;
				switch(_dataType){
					case "IdCard" :
					case "Date" :
					case "Repeat" :
					case "Range" :
					case "Compare" :
					case "Custom" :
					case "Group" : 
					case "Limit" :
					case "LimitB" :
					case "SafeString" :
					case "Filter" :
						if(!eval(this[_dataType]))	{
							this.AddError(i, _message);
						}
						break;
					default :
						if(!this[_dataType].test(value)){
							this.AddError(i, _message);
						}
						break;
				}
			}
		}
	},
	IsIdCard : function(number) {
		var date, Ai;
		var verify = "10x98765432";
		var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
		var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
		var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
		if(re == null) return false;
		if(re[1] >= area.length || area[re[1]] == "") return false;
		if(re[2].length == 12){
			Ai = number.substr(0, 17);
			date = [re[9], re[10], re[11]].join("-");
		}
		else{
			Ai = number.substr(0, 6) + "19" + number.substr(6);
			date = ["19" + re[4], re[5], re[6]].join("-");
		}
		if(!this.IsDate(date, "ymd")) return false;
		var sum = 0;
		for(var i = 0;i<=16;i++){
			sum += Ai.charAt(i) * Wi[i];
		}
		Ai +=  verify.charAt(sum%11);
		return (number.length ==15 || number.length == 18 && number == Ai);
	}
}